/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.api.utilisateur;


import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.event.EventListenerList;
import fr.univ_tln.inf9.exaplanning.api.administration.Ticket;
import fr.univ_tln.inf9.exaplanning.api.administration.event.ListTicketChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.administration.event.ListTicketListener;
import fr.univ_tln.inf9.exaplanning.api.administration.event.TicketAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.administration.event.TicketRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.db.EntityInfo;
import fr.univ_tln.inf9.exaplanning.api.db.SessionManager;

/**
 * @author pivi
 * @author emilien (persistance)
 */
@EntityInfo(	collections = { },
		key = "email",
		methods = { "getMail", "getFirst_name", "getName" }, 
		rows = { "email", "first_name", "last_name" },
		table = "teacher_view",
		variables = { "first_name", "name" })
public class Teacher extends User {

	private List<String> ticketsTeacher = new LinkedList<String>();
	
	/**
	 * Constructeur sans Ticket car les tickets sont ajoutés aprés
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public Teacher(String name,String first_name,String mail) throws IOException, SQLException {
		super(true);
		this.name = name;
		this.first_name = first_name;
		this.mail = mail;
		
		listeners = new EventListenerList();
	}
	
	
	/**
	 * 
	 * @param mail
	 * @param first_name
	 * @param last_name
	 * @param b
	 * @throws IOException
	 * @throws SQLException
	 */

	private Teacher(String mail, String first_name, String last_name, boolean b) throws IOException, SQLException {
		super(false);
		this.mail = mail;
		this.first_name = first_name;
		this.name = last_name;
	}

	/**
	 * @return the ticketsTeacher
	 */
	public List<String> getTicketsTeacher() {
		return ticketsTeacher;
	}

	/**
	 * @param ticketsTeacher the ticketsTeacher to set
	 * 
	 * fireListTicketChanged() alerte que la liste de tickets à changer
	 */
	public void setTicketsTeacher(List<String> ticketsTeacher) {
		this.ticketsTeacher = ticketsTeacher;
		
		fireListTicketChanged();
	}

	/**
	 * @param arg0
	 * @return
	 * @see java.util.List#add(java.lang.Object)
	 * 
	 */
	public boolean add(Ticket arg0) {
		fireAddTicketToList();
		return ticketsTeacher.add(arg0.getId());
	}


	/**
	 * @param arg0
	 * @return
	 * @see java.util.List#remove(java.lang.Object)
	 */
	public boolean remove(Object arg0) {
		fireRemoveTicketToList();
		return ticketsTeacher.remove(arg0);
	}



	public String ticketTeachertoString() {
		return "Teacher [ticketsTeacher=" + ticketsTeacher + "]";
	}
	
	public static Teacher findByKey(String key) throws IOException, SQLException {
    	List<List<String>> data = SessionManager.getInstance().getData(Student.class, key);
    	
    	List<String> variables = data.get(0);
    	Teacher result = new Teacher(variables.get(0), variables.get(1), variables.get(2), false);
    	
    	return result;
	}

	/****************************************************************************************************************/
	/****************************************			LISTENER				*************************************/
	/****************************************************************************************************************/

	
	public void addListTicketListener(ListTicketListener listener){
		listeners.add(ListTicketListener.class, listener);
	}
	
	public void removeListTicketListener(ListTicketListener l){
		 listeners.remove(ListTicketListener.class, l);
	}
	
	
	
	/**
	 * Methode pour avertir qu'un changement de liste à eu lieu
	 */

	private void fireListTicketChanged() {
		ListTicketListener[] listenerList = (ListTicketListener[])listeners.getListeners(ListTicketListener.class);
		
		for(ListTicketListener listener : listenerList){
			listener.ListTicketChanged(new ListTicketChangedEvent(this, getTicketsTeacher()));
		}
		
	}

	
	/**
	 * Méthode pour avertir qu'un ticket a été ajouté à la liste
	 */
	private void fireAddTicketToList() {
		ListTicketListener[] listenerList = (ListTicketListener[])listeners.getListeners(ListTicketListener.class);
		
		for(ListTicketListener listener : listenerList){
			listener.TicketAddedToList(new TicketAddedToListEvent(this, ticketTeachertoString()));
		}
		
	}
	
	
	/**
	 * Méthode pour avertir qu'un ticket a été supprimer de la liste
	 */
	private void fireRemoveTicketToList() {
		ListTicketListener[] listenerList = (ListTicketListener[])listeners.getListeners(ListTicketListener.class);
		
		for(ListTicketListener listener : listenerList){
			listener.TicketRemovedToList(new TicketRemovedToListEvent(this, ticketTeachertoString()));
		}
		
	}
	
}
